/*
 * @Author: 西索酱
 * @Date: 2021-07-04 10:23:46
 * @LastEditTime: 2021-07-04 15:46:46
 * @LastEditors: 西索酱
 * @Description: 
 * @FilePath: \snake_demo2\代码\vscripts\console.ts
 * 你也在看我的代码吗？
 */

export class Console {
    /**指令的列表 */
    private list:single_code_info[]= [
        {code:'rerun' , button: true, desc:'重启游戏' },
        {code:'reload', button: true, desc:'重载脚本' },
    ]
    constructor() {
        // print(this,this.list)
        // for (const key in this.list) {
        //     print(key,this.list[key].code)
        // }
        CustomGameEventManager.RegisterListener('p_cosole_require',(id,event)=>this.sentInfo())
        this.list.forEach(ele => {
            CustomGameEventManager.RegisterListener(ele.code,(id,event)=>this[ele.code]())
        });
        print('console已载入')
    }

    log(msg:string){
        CustomGameEventManager.Send_ServerToAllClients('s_console_code_log',{msg:msg})
    }

    searchCode(code:string):single_code_info {
        for (let index = 0; index < this.list.length; index++) {
            if(this.list[index].code==code){
                return this.list[index]
            }
        }
        return {code:'',button:false,desc:''}
    }

    sentInfo(){
        // CustomNetTables.SetTableValue('s_console_code_list','list',this.list)
        CustomGameEventManager.Send_ServerToAllClients('s_console_code_list',this.list)
        this.log('更新了指令清单')
    }

    rerun(userId: EntityIndex, event: { PlayerID: PlayerID; }): void{
        this.log(this.searchCode('rerun').desc)
        SendToServerConsole('restart')
    }

    reload(userId: EntityIndex, event: { PlayerID: PlayerID; }): void{
        SendToServerConsole('script_reload')
        this.log(this.searchCode('reload').desc)
    }
}